<template>
    <div :style="{backgroundColor: '#F6F6F6',listStyle:'none'}">
        <van-nav-bar
                :fixed="fixed"
                title="修改收货地址"
                left-text="返回"
                left-arrow
                @click-left="onClickLeft"
        />
        <div :style="{marginTop:'46px'}">
            <van-address-edit
                    :area-list="areaList"
                    show-postal
                    show-set-default
                    show-search-result
                    :search-result="searchResult"
                    :address-info="addressInfo"
                    @save="onSave"
            />
        </div>
        <div :style="{height:'330px',backgroundColor:'#F6F6F6'}"></div>
    </div>
</template>

<script>
    import areaList from './areaList'
    import Vue from 'vue'
    import { AddressEdit,NavBar,Toast} from 'vant';
    import {Edit,UserAddressEdit} from "../api/data";
    Vue.use(AddressEdit,NavBar);
    export default {
        data() {
            return {
                fixed:true,
                areaList,
                searchResult: [],
                addressInfo:''
            }
        },
        methods: {
            onSave(content) {
                let id = content.id
                let name = content.name
                let tel = content.tel
                let province = content.province
                let county = content.city
                let city = content.county
                let address = content.addressDetail
                let postalcode = content.postalCode
                let code = (content.isDefault)?1:0
                let area_code = content.areaCode
                UserAddressEdit(id,name,tel,province,county,city,address,postalcode,code,area_code).then(res=>{
                   if (res.data.code==1){
                       Toast('修改成功！')
                       this.$router.back()
                   }else{
                        Toast('修改失败！')
                   }
                })
            },
            onClickLeft() {
                this.$router.back()
            }
        },
        created(){
            let id = this.$route.query.editid
            Edit(id).then(res=>{
                console.log(res.data)
                if (res.data.code == '10001') {
                    Toast('登录已失效，请重新登录！')
                    this.$router.push({path:`/login`})
                    localStorage.removeItem('token')
                    return
                }
                this.name = res.data.data.name
                let addressInfo = new Object;
                this.areaList = areaList;
                var iscode = (res.data.data.type)?true:false
                addressInfo = {
                    areaCode:res.data.data.area_code,
                    id:res.data.data.id,
                    name:res.data.data.name,
                    tel:res.data.data.phone,
                    province:res.data.data.province,
                    city:res.data.data.district,
                    county:res.data.data.city,
                    addressDetail:res.data.data.address,
                    postalCode:res.data.data.code,
                    isDefault:iscode
                }
                this.addressInfo = addressInfo;
            })
        }
    }
</script>

<style scoped>
    .title{
        border-bottom: 1px solid #BFBFC0;
    }
</style>
